package com.hb.trade.api.routine.sms.aspect;

import cn.hutool.core.bean.BeanUtil;
import com.hb.core.pojo.center.au.dto.AgencyUserBidDTO;
import com.hb.core.pojo.center.au.dto.ZappUserDTO;
import com.hb.core.pojo.enums.SmsTypeEnum;
import com.hb.core.pojo.project.calibration.dto.BidWinResultDTO;
import com.hb.core.pojo.project.deal.dto.BidCandidateDTO;
import com.hb.core.pojo.project.file.dto.BidFileInfoDTO;
import com.hb.core.pojo.project.notice.dto.NoticeDTO;
import com.hb.core.pojo.project.notice.entity.Notice;
import com.hb.core.pojo.project.order.dto.UserOrderDTO;
import com.hb.core.pojo.project.proj.entity.AbstractProject;
import com.hb.core.pojo.project.section.dto.ProblemDTO;
import com.hb.core.pojo.project.section.dto.SupplyBidSignupDTO;
import com.hb.core.pojo.project.section.dto.TradeSecondQuotationBidInfoDto;
import com.hb.core.pojo.project.section.entity.SupplyBidSignup;
import com.hb.trade.api.routine.sms.annotation.SmsSend;
import com.hb.trade.api.routine.sms.service.SmsSendService;
import com.hb.trade.client.project.NoticeClient;
import com.hb.trade.client.project.ProjectClient;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.fzzn.core.model.msg.ResponseDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;


/**
 * @author yuezheng
 */
@Aspect
@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)
@Component
public class SmsSendAspect {

    @Autowired
    private NoticeClient noticeClient;
    @Autowired
    private ProjectClient projectClient;
    @Autowired
    private SmsSendService smsSendService;

    @Value("${sms.flog}")
    private boolean smsFlog;

    @Pointcut("@annotation(com.hb.trade.api.routine.sms.annotation.SmsSend)")
    public void SmsAspect() {

    }

    @Around(value = "SmsAspect() && @annotation(smsSend)")
    public ResponseDto doAround(ProceedingJoinPoint joinPoint, SmsSend smsSend) throws Throwable {

        Object[] args = joinPoint.getArgs();
        Object result = joinPoint.proceed();

        ResponseDto responseDto = BeanUtil.copyProperties(result, ResponseDto.class);
        SmsTypeEnum smsTypeEnum = SmsTypeEnum.getTypeByValue(smsSend.type());

        switch (smsTypeEnum) {
            case TENDERREGISTER:
                if (responseDto.getStatus() == 200 && smsFlog) {
                    ZappUserDTO zappUserDTO = BeanUtil.copyProperties(args[0], ZappUserDTO.class);
                    smsSendService.tenderRegister(zappUserDTO, SmsTypeEnum.TENDERREGISTER.getTemplate());
                }
                break;

            case REGISTER:
                if (responseDto.getStatus() == 200 && smsFlog) {
                    AgencyUserBidDTO agencyUserBidDTO = BeanUtil.copyProperties(args[0], AgencyUserBidDTO.class);
                    smsSendService.register(agencyUserBidDTO, SmsTypeEnum.REGISTER.getTemplate());
                }
                break;

            case INVITATION:
                if (responseDto.getStatus() == 200 && smsFlog) {
                    NoticeDTO noticeDTO = BeanUtil.copyProperties(args[0], NoticeDTO.class);
                    Notice notice = noticeClient.findNoticeById(noticeDTO).getData();
                    AbstractProject abstractProject = new AbstractProject();
                    abstractProject.setId(notice.getProjectId());
                    AbstractProject data = projectClient.projectDetail(abstractProject).getData();
                    if (data.getCommonType().equals(2) && notice.getNoticeNature().equals(1)) {
                        smsSendService.release(notice, SmsTypeEnum.INVITATION.getTemplate());
                    } else if (!notice.getNoticeNature().equals(1)) {
                        smsSendService.noticeRelease(notice, SmsTypeEnum.NOTICE.getTemplate());
                    }
                }
                break;

            case REGISTRATION:
                if (responseDto.getStatus() == 200 && smsFlog) {
                    SupplyBidSignupDTO supplyBidSignupDTO = BeanUtil.copyProperties(args[0], SupplyBidSignupDTO.class);
                    smsSendService.saveSupplyBidSignupInfo(supplyBidSignupDTO, SmsTypeEnum.REGISTRATION.getTemplate());
                }
                break;

            case SALE:
                if (responseDto.getStatus() == 200 && smsFlog) {
                    BidFileInfoDTO bidFileInfoDTO = BeanUtil.copyProperties(args[0], BidFileInfoDTO.class);
                    smsSendService.deploy(bidFileInfoDTO.getId(), SmsTypeEnum.SALE.getTemplate());
                }
                break;

            case MARGIN:
                if (responseDto.getStatus() == 200 && smsFlog) {
                    UserOrderDTO userOrderDTO = BeanUtil.copyProperties(args[0], UserOrderDTO.class);
                    smsSendService.submitBond(userOrderDTO, SmsTypeEnum.MARGIN.getTemplate());
                }
                break;

            case OPENING:
                break;

            case PUBLICITY:
                if (responseDto.getStatus() == 200 && smsFlog) {
                    BidCandidateDTO bidCandidateDTO = BeanUtil.copyProperties(args[0], BidCandidateDTO.class);
                    smsSendService.publishCandidate(bidCandidateDTO, SmsTypeEnum.PUBLICITY.getTemplate());
                }
                break;

            case RESULT:
                if (responseDto.getStatus() == 200 && smsFlog) {
                    BidWinResultDTO bidWinResultDTO = BeanUtil.copyProperties(args[0], BidWinResultDTO.class);
                    smsSendService.releaseBidWinResult(bidWinResultDTO, SmsTypeEnum.RESULT.getTemplate());
                }
                break;

            case QUESTION:
                if (responseDto.getStatus() == 200 && smsFlog) {
                    ProblemDTO problemDTO = BeanUtil.copyProperties(args[0], ProblemDTO.class);
                    smsSendService.sendOutProblem(problemDTO, SmsTypeEnum.QUESTION.getTemplate());
                }
                break;

            case APPROVED:
                if (responseDto.getStatus() == 200 && smsFlog) {
                    SupplyBidSignup passSupplyBidSignup = BeanUtil.copyProperties(args[0], SupplyBidSignup.class);
                    smsSendService.passExamine(passSupplyBidSignup, SmsTypeEnum.APPROVED.getTemplate());
                }
                break;

            case NOTAPPROVED:
                if (responseDto.getStatus() == 200 && smsFlog) {
                    SupplyBidSignup failSupplyBidSignup = BeanUtil.copyProperties(args[0], SupplyBidSignup.class);
                    smsSendService.failExamine(failSupplyBidSignup, SmsTypeEnum.NOTAPPROVED.getTemplate());
                }
                break;

            case QUOTE:
                if (responseDto.getStatus() == 200 && smsFlog) {
                    TradeSecondQuotationBidInfoDto tradeSecondQuotationBidInfo = BeanUtil.copyProperties(args[0], TradeSecondQuotationBidInfoDto.class);
                    if (tradeSecondQuotationBidInfo.getEndType().equals(String.valueOf(2))) {
                        smsSendService.saveSecQuoBidInfo(tradeSecondQuotationBidInfo, SmsTypeEnum.FINISH.getTemplate());
                    } else {
                        smsSendService.saveSecQuoBidInfo(tradeSecondQuotationBidInfo, SmsTypeEnum.QUOTE.getTemplate());
                    }
                }
                break;
        }
        return responseDto;
    }
}
